package Lintcode;

import java.util.Scanner;

/**
 * Question: https://www.lintcode.com/zh-cn/old/problem/first-position-of-target/
 * Solution:
 * Time: 2018/5/17
 * Coder: Ksxy
**/
public class FrstPositionOfTarget {
    static Scanner scanner = null;

    static {
        scanner = new Scanner(System.in);
    }

    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 3, 4, 5, 10};
        int target = scanner.nextInt();
        int r = binarySearch(nums, target);
        System.out.println(r);
    }

    public static int binarySearch(int[] nums, int target) {
        int left = 0, right = nums.length-1;
        int firstPosition = Integer.MAX_VALUE;
        while (left <= right){
            int index = (left + right)/2;
            if(target == nums[index]){
                firstPosition = firstPosition < index ? firstPosition : index;
            }
            if(target > nums[index]){
                left = index+1;
            }else {
                right = index-1;
            }
        }
        return firstPosition == Integer.MAX_VALUE ? -1 : firstPosition;
    }


}
